# -*- conding:UTF-8 -*-

'''
@ Project: P7_project
@ File: jwt_util.py
@ Author: 罗婷
@ Date：2022/5/30 11:04
生成token  校验token
'''
from datetime import datetime, timedelta

import jwt
from flask import current_app


def gengrate_token(payload, expiry, algorithm='HS256'):
    """生成token
    :param payload: dict 载荷
    :param expiry: datetime 有效期
    :param secret: 盐
    :return: token"""
    secret = current_app.config.get('SECRET_KEY')
    _payload = {'exp': expiry}
    _payload.update(payload)
    token = jwt.encode(_payload, secret, algorithm)
    return token

def check_token(token):
    """
    校验jwt
    :param token: token值
    :param secret: 盐
    :return: payload 载荷
    """
    secret = current_app.config.get('SECRET_KEY')
    payload = jwt.decode(token, secret, algorithms='HS256')
    # print('校验jwt时返回的payload', payload)
    return payload

def _gengrate_token(payload, algorithm='HS256'):
    #
    expiry = datetime.now()+ timedelta(hours=2)
    token = gengrate_token(payload, expiry)



    secret = current_app.config.get('SECRET_KEY')
    _payload = {'exp': expiry}
    _payload.update(payload)


